<template>
  <div class="login_box">
    <div class="mask" @click="changloginshow(false)"></div>
    <div class="login">
      <div class="close" @click="changloginshow(false)">
        <img src="../assets/img/close.png" alt="" />
      </div>
      <div class="nav">
        <a
          href="###"
          :class="{ cur: wechatshow }"
          @click="changwechatshow(true)"
          >手机号码登陆</a
        >
        |
        <a href="###" :class="{ cur: !wechatshow }" @click="wechatClick"
          >微信登陆</a
        >
      </div>
      <!-- 手机登陆 -->
      <div class="content" v-show="wechatshow">
        <div class="row">
          <input type="text" placeholder="请输入手机号" v-model="phoneNum" />
        </div>
        <div class="row">
          <!-- 滑块图片组件 -->
          <slide-verify
            :l="42"
            :r="20"
            :w="372"
            :h="130"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
            :style="{ width: '100%' }"
            class="slide-box"
            ref="slideBlock"
            :slider-text="msg"
            :imgs="arr"
            :accurac="accuracy"
          ></slide-verify>
        </div>
        <div class="row">
          <input type="text" placeholder="请输入短信验证码" v-model="code" />
          <!-- <button class="codebtn" @click="codebtn">获取验证码</button> -->

          <button class="codebtn" @click="codebtn" v-show="isShow">
            获取验证码
          </button>
          <button class="codebtn" v-show="!isShow">{{ count }}s</button>
        </div>
        <div class="row">
          <button class="loginbtn" @click="loginbtn">登陆</button>
        </div>
      </div>
      <!-- 微信登陆 -->
      <div class="wechat" v-show="!wechatshow" id="wechat"></div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapActions, mapState } from "vuex";
import {
  sendSMSApi,
  phoneReginApi,
  bandingApi,
} from "../request/api";
import { checkPhone } from "../utils/index";

export default {
  data() {
    return {
      show: true,
      msg: "向右滑动",
      // 滑块图片
      arr: [
        require("../assets/img/integral-04.afadcbdf.png"),
        require("../assets/img/integral-03.9870f3f1.png"),
        require("../assets/img/integral-02.150d92a1.png"),
      ],
      // 手机号码
      phoneNum: "",
      // 计时器
      count: 5,
      // 是否显示计时器
      isShow: true,
      // 误差值
      accuracy: 10,
      // 验证码
      code: "",
    };
  },
  computed: {
    ...mapState({
      wechatshow: (state) => state.WechatStatus.wechatshow,
    }),
  },
  methods: {
    ...mapMutations({
      changloginshow: "Login/changloginshow",
      changLoginStatus: "LoginStatus/changLoginStatus",
      changwechatshow: "WechatStatus/changwechatshow",
    }),
    ...mapActions({
      asyncChangtoastshow: "ToastStatus/asyncChangtoastshow",
      asyncchanguserInfo:"UserInfo/asyncchanguserInfo"
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },

    // 验证条件
    yanzhen() {
      // 验证手机号
      if (!checkPhone(this.phoneNum.trim())) {
        // alert("请输入正确的手机好嘛");
        this.asyncChangtoastshow({
          show: true,
          msg: "请输入正确的手机好嘛",
          type: "danger",
        });
        return;
      }
      // 验证滑块
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        // alert("请滑动滑块验证");
        this.asyncChangtoastshow({
          show: true,
          msg: "请滑动滑块验证",
          type: "warning",
        });
        return;
      }
      return true;
    },

    // 倒计时
    dingshiqi() {
      this.isShow = false;
      let timer = setInterval(() => {
        this.count--;
        if (this.count <= 0) {
          this.count = 5;
          clearInterval(timer);
          this.isShow = true;
        }
      }, 1000);
    },

    // 获取验证码
    async codebtn() {
      // 验证信息
      if (!this.yanzhen()) return;
      // 发送请求     18312081177
      let res = await sendSMSApi({ phone: this.phoneNum });
      if (!res) return;
      this.dingshiqi();
    },

    // 登陆
    async loginbtn() {
      // 条件验证
      if (!this.yanzhen()) return;
      if (this.code.trim() == "") {
        this.asyncChangtoastshow({
          show: true,
          msg: "请输入验证码",
          type: "warning",
        });
        return;
      }
      // 发送请求
      // 分两种情况：1、是没有uuid就是普通登陆；2、有uuid就绑定手机号
      let res = null;
      let uuid = localStorage.getItem("uuid");
      if (uuid) {
        res = await bandingApi({
          verifyCode: this.code,
          phone: this.phoneNum,
          uuid,
        });
      } else {
        res = await phoneReginApi({
          verifyCode: this.code,
          phone: this.phoneNum,
        });
      }

      if (!res) return;
      // 1.弹出提示框
      this.asyncChangtoastshow({
        show: true,
        msg: "登陆成功",
        type: "success",
      });
      // 2.关闭登陆框
      this.changloginshow(false);
      // 3.存token
      localStorage.setItem("token", res["x-auth-token"]);
      // 4.切换tobar
      this.changLoginStatus(true);

      // 5.清除code、uuid
      this.$route.query.code && this.$router.push(this.$route.path);
      uuid && localStorage.removeItem("uuid");

      // 6.获取用户信息
      this.asyncchanguserInfo();
    },

    wechatClick() {
      this.changwechatshow(false);

      // 微信登录第一步：申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "wechat",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlIHsNCiAgZGlzcGxheTogbm9uZTsNCn0NCg0KLmltcG93ZXJCb3ggLndycF9jb2RlIGltZyB7DQogIHdpZHRoOiA2MCU7DQogIGhlaWdodDogYXV0bzsNCn0=",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_box {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;

  .mask {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }

  .login {
    width: 555px;
    height: 423px;
    position: absolute;
    inset: 0;
    margin: auto;
    background-image: url("../assets/img/login-box-bg.png");

    .close {
      position: absolute;
      top: 13px;
      right: 59px;
      cursor: pointer;
    }

    .nav {
      height: 50px;
      text-align: center;
      padding-top: 30px;

      a {
        text-decoration: none;
        margin: 0 20px;
        color: gray;
        font-size: 18px;
        &.cur {
          color: black;
        }
      }
    }

    .content {
      padding-top: 10px;
      box-sizing: border-box;
      width: 68%;
      height: 68%;
      position: absolute;
      inset: 0;
      margin: auto;

      .row {
        display: flex;
        height: 50px;
        margin: 20px 0;
        input {
          flex: 1;
          outline: none;
        }
      }

      .codebtn {
        background-color: rgb(10, 50, 142);
        border: none;
        color: white;
        cursor: pointer;
        width: 79px;
      }
      .loginbtn {
        flex: 1;
        background-color: rgb(10, 50, 142);
        border: none;
        height: 50px;
        color: white;
        cursor: pointer;
      }
    }

    .wechat {
      padding-top: 10px;
      box-sizing: border-box;
      width: 68%;
      height: 68%;
      position: absolute;
      inset: 0;
      margin: auto;
      text-align: center;
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>